剛開始學 Terraform 是從 AWS cloud 開始,會覺得 module 這東西真好用,咻一下就幫你把複雜的 resources 還有相關資源都建立起來了。但 GCP 官方 module 喜歡考驗你的真本領,會在 module 設定挑戰。這次的經驗就是我嘗試在 build Cloud SQL,並且我希望將 cloud_sql_instance 跟 cloud SQL 綁定,但遇到他要求 string error
terraform plan
╷
│ Error: Invalid value for input variable
│
│ on cloud-run.tf line 96, in module "cloud_run_backend":
│ 96: volumes = [
│ 115: {
│ 116: name = "cloud_sql_instance"
│ 117: cloud_sql_instance = {
│ 118: instances = ["xxx-sql"]
│ 119: }
│ 120: secret = null
│ 121: }
│ 122: ]
│
│ The given value is not suitable for module.cloud_run_backend.var.volumes declared at .terraform/modules/cloud_run_backend/modules/v2/variables.tf:199,1-19: element 2: attribute "cloud_sql_instance": attribute
│ "instances": string required.
╵
但如果給 String 他會跳出以下 error。
│
│ on .terraform/modules/cloud_run_backend/modules/v2/main.tf line 216, in resource "google_cloud_run_v2_service" "main":
│ 216: instances = cloud_sql_instance.value["instances"]
│
│ Inappropriate value for attribute "instances": set of string required.
由於怎麼改都會是錯的,認真看了一下 module
variable "volumes" {
type = list(object({
name = string
secret = object({
secret = string
default_mode = optional(string)
items = optional(object({
path = string
version = optional(string)
mode = optional(string)
}))
})
cloud_sql_instance = optional(object({
instances = optional(string)
}))
但是從官方文件來看需求是 list
volumes {
name = "cloudsql"
cloud_sql_instance {
instances = [google_sql_database_instance.instance.connection_name]
}
}
所以我們要自行修改 module 成
instances = optional(list(string))